<template>
  <div class="count">
    <h2>当前求和为：{{ sum }}，放大10倍后：{{ bigSum }}</h2>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="add">+</button>
    <button @click="minus">-</button>
    <button @click="OddAdd(n)">当前和为奇数再+</button>
    <button @click="waitAdd(n)">等一等再+</button>
    <button @click="serverAdd">问问服务器再+</button> <br>
    <h5>作者：
      <span>{{ school }}</span>&nbsp;
      <span>{{ upperSchool }}</span>
    </h5>
    <hr>
    <button @click="test">测试一下mapState</button>
  </div>
</template>

<script>
  import {mapState,mapGetters,mapActions} from 'vuex'
  export default {
    name:'Count',
    data() {
      return {
        n:1 //用户选择的数字
      }
    },
    computed: {
      ...mapState(['sum','school']),
      ...mapGetters(['bigSum','upperSchool'])
    },
    methods: {
      add(){
        this.$store.commit('JIA',this.n)
      },
      minus(){
        this.$store.commit('JIAN',this.n)
      },

      /* OddAdd(){
        this.$store.dispatch('jijia',this.n)
      }, 
      waitAdd(){
        this.$store.dispatch('dengjia',this.n)
      },
      serverAdd(){
        this.$store.dispatch('fuwujia')
      },*/

      // 对象写法
      ...mapActions({
        OddAdd:'jijia',
        waitAdd:'dengjia',
        serverAdd:'fuwujia'
      }),

      // 数组写法
      // ...mapActions(['jijia','dengjia','fuwujia']),
      
      test(){
        let x = mapState(['sum','school'])
        console.log(x)
      }
    }
  }
</script>

<style>
  .count {
    background-color: skyblue;
    border-radius: 10px;
    box-shadow: 0 0 10px;
    padding: 10px;
  }
  button {
    margin: 0 5px;
  }
  button,select {
    height: 25px;
  }
</style>